#!/bin/bash
CURRENT_DIR=$(
    cd $(dirname ${BASH_SOURCE:-$0})
    pwd
)

if [ -n "$ASCEND_INSTALL_PATH" ]; then
    _ASCEND_INSTALL_PATH=$ASCEND_INSTALL_PATH
elif [ -n "$ASCEND_HOME_PATH" ]; then
    _ASCEND_INSTALL_PATH=$ASCEND_HOME_PATH
else
    if [ -d "$HOME/Ascend/ascend-toolkit/latest" ]; then
        _ASCEND_INSTALL_PATH=$HOME/Ascend/ascend-toolkit/latest
    else
        _ASCEND_INSTALL_PATH=/usr/local/Ascend/ascend-toolkit/latest
    fi
fi
source $_ASCEND_INSTALL_PATH/bin/setenv.bash
export DDK_PATH=$_ASCEND_INSTALL_PATH
export NPU_HOST_LIB=$_ASCEND_INSTALL_PATH/$(arch)-$(uname -s | tr '[:upper:]' '[:lower:]')/devlib

export RANK_SIZE=8
export USED_CORE_NUM=16
export P_VALUE=2
export P_VALUE_COMM=3
export CALL_TIMES=1
export IF_CHECK=1
export PROMPT_LEN=4000

# mpi
export OMPI_ALLOW_RUN_AS_ROOT=1
export OMPI_ALLOW_RUN_AS_ROOT_CONFIRM=1

# export ASCEND_SLOG_PRINT_TO_STDOUT=1
# export ASCEND_GLOBAL_LOG_LEVEL=3         ### 0 debug, 1 info, 2 warning, 3 error
# export ASCEND_GLOBAL_EVENT_ENABLE=0

# export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/simulator/Ascend910B/

function main {
    # 1. 清除遗留生成文件和日志文�?    
    rm -rf $HOME/ascend/*
    rm ./input/*.bin
    rm ./output/*.bin

    # 2. 生成输入数据和真值数�?    
    cd $CURRENT_DIR
    python3 scripts/gen_data.py
    if [ $? -ne 0 ]; then
        echo "ERROR: generate input data failed!"
        return 1
    fi
    echo "INFO: generate input data success!"

    # 3. 编译acl可执行文�?    
    cd $CURRENT_DIR
    rm -rf build
    mkdir -p build
    cd build
    cmake ../src -DCMAKE_SKIP_RPATH=TRUE
    if [ $? -ne 0 ]; then
        echo "ERROR: cmake failed!"
        return 1
    fi
    echo "INFO: cmake success!"
    make
    if [ $? -ne 0 ]; then
        echo "ERROR: make failed!"
        return 1
    fi
    echo "INFO: make success!"

    # 4. 运行可执行文�?    
    # export LD_LIBRARY_PATH=$_ASCEND_INSTALL_PATH/aarch64-linux/simulator/Ascend910B1/lib/:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH=$_ASCEND_INSTALL_PATH/opp/vendors/customize/op_api/lib:$LD_LIBRARY_PATH
    cd $CURRENT_DIR/output
    echo "INFO: execute op!"

    # mpirun -np 8 --allow-run-as-root msprof --application="./execute_quantmatmulv2" --output=../msprof --ascendcl=on --runtime-api=on --task-time=on --aicpu=on --ai-core=on --runtime-api=on --hccl=on --aic-metrics=PipeUtilization
    # mpirun -np 8 --allow-run-as-root msprof op --aic-metrics=Default --output=../msprof ./execute_quantmatmulv2
    mpirun -np 8 --allow-run-as-root ./execute_quantmatmulv2


    # if [ $? -ne 0 ]; then
    #     echo "ERROR: acl executable run failed! please check your project!"
    #     return 1
    # fi
    echo "INFO: acl executable run success!"
    # mpirun -np 8 ./execute_quantmatmulv2 --allow-run-as-root

    # 5. 比较真值文�?    
    if [ $IF_CHECK -eq 0]; then
        echo "Skip check result!"
        return 0
    fi

    cd $CURRENT_DIR
    python3 scripts/verify_result.py
    if [ $? -ne 0 ]; then
        echo "ERROR: verify result failed!"
        return 1
    fi
}



main
